// pages/applyrefund/applyrefund.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: host,
    orderId:'', 
    textValue:'',
    detailDic:{},
      choseImgList:[]
  },
  textInput(e){
     
    this.setData({
      textValue:e.detail.value
    }) 
  },
  addImages(){
    let that=this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      success: function (res) {
      
        network.uploadFileLoading("/common/upload", res.tempFiles[0].path, res => {
          let data = JSON.parse(res.data);
          let imgUrl = data.fileName;  
          
          let imgArr=that.data.choseImgList;
          
          imgArr.push(imgUrl); 
          that.setData({
            choseImgList:imgArr
          })
        })
      }
    });


    
  },
  DelImg(e) {
    console.log(e)
    // let theurl = e.currentTarget.dataset.url;
    wx.showModal({
      title: '删除图片',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          
          let imgList = this.data.choseImgList;
            imgList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              choseImgList: imgList
            })
        }
      }
    })
  },
  postData() {
    let that = this;
    if(!that.data.orderId){
      wx.showToast({
        title: '未获取到订单ID!',
        icon:'none'
      })
      return;
    }
    if(!that.data.textValue){
      wx.showToast({
        title: '请填写退款原因!',
        icon:'none'
      })
      return;
    }
    network.requestPost(host + '/weiXinPayment/appeal-refund', {
      "customerPaymentRecordId":that.data.orderId,
      "appealRefundImage":that.data.choseImgList.join(','),
      "appealRefundReason":that.data.textValue
    }, res => {
      console.log("res1", res)
      if (res.data.code == 200) {
       wx.showToast({
         title: '申请已提交',
       })
       setTimeout(() => {
         wx.navigateBack({
           delta: 0,
         })
       }, 1500);
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none"
        })
      }
    })
  },
  getDetail() {
    let that = this;
    console.log("===="+this.data.orderId)
    network.requestGet(host + '/xcx/customerPaymentRecord/getInfo/'+this.data.orderId, {
      
    }, res => {  
       
      if (res.data.code == 200) {
//         appealRefundImage: ""
// appealRefundNumber: 0
// appealRefundReason: ""
// detailDic:{},
//     textValue:'',
//       choseImgList:[]
        let arr=res.data.data.appealRefundImage.split(',')
        
        that.setData({ 
          detailDic:res.data.data,
          textValue:res.data.data.appealRefundReason,
          choseImgList:arr
        })
        
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     if(options.orderId){
        this.setData({
          orderId:options.orderId
        })
     }
     this.getDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})